package vn.vfriends.payment.bean;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vn.vfriends.id.ws.User;
import vn.vfriends.payment.jpa.entity.Transaction;
import vn.vfriends.payment.service.AccountService;

/**
 *
 * @author tuan@vfriends.vn
 */
@ManagedBean
@ViewScoped
public class TransactionBean implements Serializable {

    private static final Logger logger = LoggerFactory.getLogger(TransactionBean.class);
    
    private Date inTransactionFromDate;
    
    private Date inTransactionToDate;
    
    private Date outTransactionFromDate;
    
    private Date outTransactionToDate;
    
    private List<Transaction> inTransactionList;
    
    private List<Transaction> outTransactionList;
    
    @Inject private AccountService accountService;

    public Date getInTransactionFromDate() {
        return inTransactionFromDate;
    }

    public void setInTransactionFromDate(Date inTransactionFromDate) {
        this.inTransactionFromDate = inTransactionFromDate;
    }

    public Date getInTransactionToDate() {
        return inTransactionToDate;
    }

    public void setInTransactionToDate(Date inTransactionToDate) {
        this.inTransactionToDate = inTransactionToDate;
    }

    public Date getOutTransactionFromDate() {
        return outTransactionFromDate;
    }

    public void setOutTransactionFromDate(Date outTransactionFromDate) {
        this.outTransactionFromDate = outTransactionFromDate;
    }

    public Date getOutTransactionToDate() {
        return outTransactionToDate;
    }

    public void setOutTransactionToDate(Date outTransactionToDate) {
        this.outTransactionToDate = outTransactionToDate;
    }

    public List<Transaction> getInTransactionList() {
        return inTransactionList;
    }

    public List<Transaction> getOutTransactionList() {
        return outTransactionList;
    }
    
    public void searchInTransaction(){
        FacesContext context = FacesContext.getCurrentInstance();
        try{
            HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("LOGGED_USER");
            this.inTransactionList = accountService.getTransactions(user.getCif(), "IN", inTransactionFromDate, inTransactionToDate);
        } catch (Exception e){
            logger.error(e.getMessage());
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", e.getMessage()));
        }
    }
    
    public void searchOutTransaction(){
        FacesContext context = FacesContext.getCurrentInstance();
        try{
            HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("LOGGED_USER");
            this.inTransactionList = accountService.getTransactions(null, "OUT", inTransactionFromDate, inTransactionToDate);
        } catch (Exception e){
            logger.error(e.getMessage());
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", e.getMessage()));
        }
    }
}
